from DrissionPage import ChromiumPage,ChromiumOptions,WebPage
# from DrissionPage import WebPage
# from DrissionPage import SessionPage
import re
import pandas as pd
import time

# 初始化页面
co = ChromiumOptions()
co.no_imgs(True).mute(True)

page = ChromiumPage(addr_or_opts=co)
# page =SessionPage()
page.set.load_mode.eager()
# page.get('https://www.zhihu.com/question/659123708')
# # page.wait.load_start()
#
#
# # 收集详情页链接
# detail_links_all = []
# prev_height = 0
#
# # 滚动页面并提取链接
# while True:
#     current_height = page.run_js('return document.documentElement.scrollHeight')
#     if current_height == prev_height:
#         break
#     page.scroll.to_bottom()
#     time.sleep(0.5)  # 等待页面加载
#     page.scroll.to_half()
#     page.scroll.to_bottom()
#     time.sleep(1.5)  # 等待页面加载
#     prev_height = current_height
#
#     # 查找每个评论的详情页链接
#     cards = page.eles('@class=List-item')
#     page.stop_loading()
#     for card in cards:
#         url = card.ele('@itemprop=url').attr('content')
#
#


title = '说说你印象最深刻的经典名句'
data=[]
# 初始化数据存储
detail_links_all = [
"https://www.zhihu.com/question/659123708/answer/3595457792",
"https://www.zhihu.com/question/659123708/answer/3592601807",
"https://www.zhihu.com/question/659123708/answer/3581029617",
"https://www.zhihu.com/question/659123708/answer/3576313386",
"https://www.zhihu.com/question/659123708/answer/3588592197",
"https://www.zhihu.com/question/659123708/answer/3585941863",
"https://www.zhihu.com/question/659123708/answer/3583318601",
"https://www.zhihu.com/question/659123708/answer/3591253661",
"https://www.zhihu.com/question/659123708/answer/3534924050",
"https://www.zhihu.com/question/659123708/answer/3574394237",
"https://www.zhihu.com/question/659123708/answer/3567142285",
"https://www.zhihu.com/question/659123708/answer/3571573258",
"https://www.zhihu.com/question/659123708/answer/3577500182",
"https://www.zhihu.com/question/659123708/answer/3577785840",
"https://www.zhihu.com/question/659123708/answer/3579024412",
"https://www.zhihu.com/question/659123708/answer/3581461179",
"https://www.zhihu.com/question/659123708/answer/3571556562",
"https://www.zhihu.com/question/659123708/answer/3577373085",
"https://www.zhihu.com/question/659123708/answer/3535769699",
"https://www.zhihu.com/question/659123708/answer/3565731063",
"https://www.zhihu.com/question/659123708/answer/3532815432",
"https://www.zhihu.com/question/659123708/answer/3569196314",
"https://www.zhihu.com/question/659123708/answer/3533208185",
"https://www.zhihu.com/question/659123708/answer/3569856429",
"https://www.zhihu.com/question/659123708/answer/3583269956",
"https://www.zhihu.com/question/659123708/answer/3575925234",
"https://www.zhihu.com/question/659123708/answer/3593186123",
"https://www.zhihu.com/question/659123708/answer/3533981908",
"https://www.zhihu.com/question/659123708/answer/3571451483",
"https://www.zhihu.com/question/659123708/answer/3579795609",
"https://www.zhihu.com/question/659123708/answer/3568546699",
"https://www.zhihu.com/question/659123708/answer/3538234221",
"https://www.zhihu.com/question/659123708/answer/3585210853",
"https://www.zhihu.com/question/659123708/answer/3534586332",
"https://www.zhihu.com/question/659123708/answer/3579988392",
"https://www.zhihu.com/question/659123708/answer/3574298274",
"https://www.zhihu.com/question/659123708/answer/3588575022",
"https://www.zhihu.com/question/659123708/answer/3583272336",
"https://www.zhihu.com/question/659123708/answer/3573739961",
"https://www.zhihu.com/question/659123708/answer/3576697880",
"https://www.zhihu.com/question/659123708/answer/3573852193",
"https://www.zhihu.com/question/659123708/answer/3578978496",
"https://www.zhihu.com/question/659123708/answer/3533842485",
"https://www.zhihu.com/question/659123708/answer/3585296613",
"https://www.zhihu.com/question/659123708/answer/3592917967",
"https://www.zhihu.com/question/659123708/answer/3596082867",
"https://www.zhihu.com/question/659123708/answer/3579967042",
"https://www.zhihu.com/question/659123708/answer/3532959194",
"https://www.zhihu.com/question/659123708/answer/3575468448",
"https://www.zhihu.com/question/659123708/answer/3533479749",
"https://www.zhihu.com/question/659123708/answer/3573171318",
"https://www.zhihu.com/question/659123708/answer/3587313666",
"https://www.zhihu.com/question/659123708/answer/3573560252",
"https://www.zhihu.com/question/659123708/answer/3572410143",
"https://www.zhihu.com/question/659123708/answer/3596116780",
"https://www.zhihu.com/question/659123708/answer/3584520188",
"https://www.zhihu.com/question/659123708/answer/3594660647",
"https://www.zhihu.com/question/659123708/answer/3533698430",
"https://www.zhihu.com/question/659123708/answer/3575395004",
"https://www.zhihu.com/question/659123708/answer/3546904519",
"https://www.zhihu.com/question/659123708/answer/3533342721",
"https://www.zhihu.com/question/659123708/answer/3583782772",
"https://www.zhihu.com/question/659123708/answer/3582162587",
"https://www.zhihu.com/question/659123708/answer/3584214844",
"https://www.zhihu.com/question/659123708/answer/3576588155",
"https://www.zhihu.com/question/659123708/answer/3576203081",
"https://www.zhihu.com/question/659123708/answer/3574600799",
"https://www.zhihu.com/question/659123708/answer/3596143328",
"https://www.zhihu.com/question/659123708/answer/3587401364",
"https://www.zhihu.com/question/659123708/answer/3585801543",
"https://www.zhihu.com/question/659123708/answer/3585430634",
"https://www.zhihu.com/question/659123708/answer/3583673285",
"https://www.zhihu.com/question/659123708/answer/3583006324",
"https://www.zhihu.com/question/659123708/answer/3581578925",
"https://www.zhihu.com/question/659123708/answer/3540926379",
"https://www.zhihu.com/question/659123708/answer/3579171544",
"https://www.zhihu.com/question/659123708/answer/3596142125",
"https://www.zhihu.com/question/659123708/answer/3579028067",
"https://www.zhihu.com/question/659123708/answer/3578880686",
"https://www.zhihu.com/question/659123708/answer/3577118572",
"https://www.zhihu.com/question/659123708/answer/3572375427",
"https://www.zhihu.com/question/659123708/answer/3575941442",
"https://www.zhihu.com/question/659123708/answer/3596275590",
"https://www.zhihu.com/question/659123708/answer/3582565561",
"https://www.zhihu.com/question/659123708/answer/3580332485",
"https://www.zhihu.com/question/659123708/answer/3574111548",
"https://www.zhihu.com/question/659123708/answer/3533640410",
"https://www.zhihu.com/question/659123708/answer/3596063322",
"https://www.zhihu.com/question/659123708/answer/3581154961",
"https://www.zhihu.com/question/659123708/answer/3596215539",
"https://www.zhihu.com/question/659123708/answer/3574166003",
"https://www.zhihu.com/question/659123708/answer/3578353728",
"https://www.zhihu.com/question/659123708/answer/3585162345",
"https://www.zhihu.com/question/659123708/answer/3582654639",
"https://www.zhihu.com/question/659123708/answer/3596228123",
"https://www.zhihu.com/question/659123708/answer/3581655074",
"https://www.zhihu.com/question/659123708/answer/3538071141",
"https://www.zhihu.com/question/659123708/answer/3579450774",
"https://www.zhihu.com/question/659123708/answer/3584600477",
"https://www.zhihu.com/question/659123708/answer/3532799939",
"https://www.zhihu.com/question/659123708/answer/3596164231",
"https://www.zhihu.com/question/659123708/answer/3596112232",
"https://www.zhihu.com/question/659123708/answer/3596191655",
"https://www.zhihu.com/question/659123708/answer/3596062172",
"https://www.zhihu.com/question/659123708/answer/3596151477",
"https://www.zhihu.com/question/659123708/answer/3596151232",
"https://www.zhihu.com/question/659123708/answer/3596142321",
"https://www.zhihu.com/question/659123708/answer/3588634464",
"https://www.zhihu.com/question/659123708/answer/3596137343",
"https://www.zhihu.com/question/659123708/answer/3596136314",
"https://www.zhihu.com/question/659123708/answer/3596122850",
"https://www.zhihu.com/question/659123708/answer/3585011785",
"https://www.zhihu.com/question/659123708/answer/3584775007",
"https://www.zhihu.com/question/659123708/answer/3596100846",
"https://www.zhihu.com/question/659123708/answer/3582558298",
"https://www.zhihu.com/question/659123708/answer/3596073788",
"https://www.zhihu.com/question/659123708/answer/3580245980",
"https://www.zhihu.com/question/659123708/answer/3596061230",
"https://www.zhihu.com/question/659123708/answer/3579035568",
"https://www.zhihu.com/question/659123708/answer/3590345923",
"https://www.zhihu.com/question/659123708/answer/3578745943",
"https://www.zhihu.com/question/659123708/answer/3586085781",
"https://www.zhihu.com/question/659123708/answer/3577849603",
"https://www.zhihu.com/question/659123708/answer/3577646776",
"https://www.zhihu.com/question/659123708/answer/3585274627",
"https://www.zhihu.com/question/659123708/answer/3534458297",
"https://www.zhihu.com/question/659123708/answer/3580575615",
"https://www.zhihu.com/question/659123708/answer/3578966366",
"https://www.zhihu.com/question/659123708/answer/3534920137",
"https://www.zhihu.com/question/659123708/answer/3584325068",
"https://www.zhihu.com/question/659123708/answer/3583688340",
"https://www.zhihu.com/question/659123708/answer/3583082218",
"https://www.zhihu.com/question/659123708/answer/3582649145",
"https://www.zhihu.com/question/659123708/answer/3582332277",
"https://www.zhihu.com/question/659123708/answer/3580741153",
"https://www.zhihu.com/question/659123708/answer/3580093920",
"https://www.zhihu.com/question/659123708/answer/3579220124",
"https://www.zhihu.com/question/659123708/answer/3577834209",
"https://www.zhihu.com/question/659123708/answer/3573593522"

]

i=1
len = len(detail_links_all)
# 访问详情页链接
for detaile_url in detail_links_all:

    tab = page.new_tab(detaile_url)
    # page.get(detaile_url)
    # tab.wait.load_start()
    # 如果觉得太慢就用这个
    # tab.wait(4.52, 5.61)
    # print(f'详情页 {detaile_url} 加载完毕')

    # 提取点赞数
    # comment = tab.ele('x://*[@id="root"]/div/main/div/div/div[3]/div[1]/div/div[2]/div/div/div/meta[2]')
    # like_count = comment.attr('content')
    # print('点赞数:', like_count)

    comment = tab.ele('@aria-label:赞同')
    # comment = page.ele('@aria-label:赞同')
    # print(comment.raw_text)
    like_count = comment.attr('aria-label')
    # 去除like_count里面的赞同
    like_count = re.sub('赞同', '', like_count)
    # 去除like_count的空格。
    like_count = like_count.strip()
    like_count = int(like_count)
    print(like_count)
    # break
#
    # 提取收藏数
    script_element = tab.ele('#js-initialData')
    print(tab.html)
    tab.stop_loading()
    favlists_list = re.findall('"favlistsCount":(\\d+),"i', tab.html)
    favlists_num = int(favlists_list[0]) if favlists_list else 0
    print('收藏数:', favlists_num)
    tab.close()  # 关闭当前标签页

    all = like_count+favlists_num
    likeRatio = like_count/all if all >0 else 0
    favRatio = favlists_num / all if all > 0 else 0

    # 保存数据
    data.append({
        'URL': detaile_url,
        '点赞数': like_count,
        '收藏数': favlists_num,
        '总数': all,
        '赞数比例': likeRatio,
        '收藏比例':favRatio
    })

    time.sleep(1)
    print("==============================="+str(i)+"/"+str(len)+"==================================")
    i=i+1


# 保存到 Excel 文件
df = pd.DataFrame(data)
df.to_excel(title + '.xlsx', index=False)

print('数据已保存到 zh.xlsx')